package com.demo.index;

import com.demo.common.model.User;
import com.demo.user.UserService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.jfinal.kit.HttpKit;

import javax.servlet.http.HttpSession;

public class LoginController extends Controller {

    @Inject
    UserService userService;


    //加载登录页面
    public void index() {
        render("login.html");
    }

    //登录系统
    public void login(){
        //String username=getPara("hm");
        //String password=getPara("pwd");
        String json = HttpKit.readData(getRequest());
        User u = FastJson.getJson().parse(json, User.class);
        User user=userService.Login(u.getHm(),u.getPwd());
        if(user!=null) {
            setSessionAttr("user",user);
            setAttr("result",1);
        }else{
            setAttr("result",2);
        }
        renderJson();
    }

    //退出系统
    public void logout(){
        HttpSession session=getSession();
        session.removeAttribute("user");
        redirect("/");
    }
}
